package com.example.demo;

import com.aspose.cells.License;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import com.aspose.words.Document;
import com.aspose.words.FontSettings;
import com.aspose.words.SaveFormat;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Properties;

public class ExcelToPdf {

    private static final String SUCCESS = "SUCCESS";
    private static final String ERROR ="ERROR";

    public static void main(String[] args) {
//        excel2pdf("C:\\tempFiles\\test2003.xls","C:\\tempFiles\\test2003.pdf");
//        excel2pdf("C:\\tempFiles\\test2007.xlsx","C:\\tempFiles\\test2007.pdf");
    }

    /**
     * excel文件导出为PDF文件
     * @param excelPath
     * @param pdfPath
     */
    public static String excel2pdf(String excelPath, String pdfPath) {
        // 验证License 若不验证则转化出的pdf文档会有水印产生
        boolean license = getLicense();
        System.out.println("license-->"+license);

        try {
            File pdfFile = new File(pdfPath); // 输出路径
            FileInputStream excelstream = new FileInputStream(excelPath);
            Workbook wb = new Workbook(excelstream);// excel路径，这里是先把数据放进缓存表里，然后把缓存表转化成PDF
            FileOutputStream fileOS = new FileOutputStream(pdfFile);
            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
            pdfSaveOptions.setOnePagePerSheet(true);//参数true把内容放在一张PDF页面上；
            wb.save(fileOS, pdfSaveOptions);
            fileOS.close();
            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
    }

    //获取认证，去除水印
    public static boolean getLicense() {
        boolean result = false;
        try {
            Resource fileRource = new ClassPathResource("license.xml");
            InputStream is = fileRource.getInputStream();
            System.out.println("fileRourse InputStream->"+is);
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}


